#include <string>
#include "io/byte_stream.h"
#include "gtest/gtest.h"

using std::string;

TEST(ByteStreamTest, TestPushPop) {
  ByteStream b;
  EXPECT_EQ(0, b.size());
  string s = "abc";
  b.Push(s.c_str(), s.size());
  EXPECT_EQ(3, b.size());
  s = "def";
  b.Push(s.c_str(), s.size());
  EXPECT_EQ(6, b.size());
  s = "ghijklmn";
  b.Push(s.c_str(), s.size());
  EXPECT_EQ(14, b.size());
  char buff[100];
  b.Pop(buff, 1);
  EXPECT_EQ(13, b.size());
  EXPECT_EQ("a", string(buff, 1));
  b.Pop(buff, 6);
  EXPECT_EQ(7, b.size());
  EXPECT_EQ("bcdefg", string(buff, 6));
  b.Pop(buff, 7);
  EXPECT_EQ(0, b.size());
  EXPECT_EQ("hijklmn", string(buff, 7));

  // Push an int.
  char a_int[sizeof(int)];
  int a = 1235;
  memcpy(a_int, (char*)&a, sizeof(int));
  b.Push(a_int, sizeof(int));
  int a_out;
  EXPECT_TRUE(b.PeekInt(&a_out));
  EXPECT_EQ(1235, a_out);
}
